package main

import (
	"go-auto_carate_yapi_json/src/service"
	"go-auto_carate_yapi_json/src/snowflake"
	"reflect"
	"time"
)

type Body struct {
	Index   int64   `json:"index"`
	Name    string  `json:"name,omitempty"`
	Desc    string  `json:"desc"`
	AddTime int64   `json:"add_time"`
	UpTime  int64   `json:"up_time"`
	List    []*List `json:"list"`
}
type List struct {
	QueryPath struct {
		Path   string      `json:"path"`
		Params []ReqParams `json:"params"`
	} `json:"query_path"`
	EditUid             int64         `json:"edit_uid"`
	Status              string        `json:"status"`
	Type                string        `json:"type"`
	ReqBodyIsJsonSchema bool          `json:"req_body_is_json_schema"`
	ResBodyIsJsonSchema bool          `json:"res_body_is_json_schema"`
	ApiOpened           bool          `json:"api_opened"`
	Index               int64         `json:"index"`
	Tag                 []Tag         `json:"tag"`
	Id                  int64         `json:"_id"`
	Title               string        `json:"title"`
	Path                string        `json:"path"`
	Method              string        `json:"method"`
	ReqQuery            []ReqQuery    `json:"req_query"`
	ReqBodyType         string        `json:"req_body_type"`
	ResBodyType         string        `json:"res_body_type"`
	ResBody             string        `json:"res_body"`
	ReqHeaders          []ReqHeader   `json:"req_headers"`
	ProjectId           int64         `json:"project_id"`
	Catid               int64         `json:"catid"`
	ReqParams           []ReqParams   `json:"req_params"`
	Uid                 int64         `json:"uid"`
	AddTime             int64         `json:"add_time"`
	UpTime              int64         `json:"up_time"`
	ReqBodyForm         []ReqBodyFrom `json:"req_body_form"`
	V                   int64         `json:"__v"`
	Desc                string        `json:"desc,omitempty"`
	Markdown            string        `json:"markdown,omitempty"`
	ReqBodyOther        string        `json:"req_body_other,omitempty"`
}

type ReqHeader struct {
	Required string `json:"required"`
	Id       string `json:"_id"`
	Name     string `json:"name"`
	Value    string `json:"value"`
}
type ReqQuery struct {
	Required string `json:"required"`
	Id       string `json:"_id"`
	Name     string `json:"name"`
	Desc     string `json:"desc"`
}

type ReqParams struct {
	Id   string `json:"_id"`
	Name string `json:"name"`
	Desc string `json:"desc"`
}

type ReqBodyFrom struct {
}
type Tag struct {
}

type Bodys []*Body

type Lists []*List

// NewBody @Description
// @Author wangsen 2022-08-03 17:48:43
// @Param name 			分类名称
// @Param desc 			api说明
// @Param lists
// @Return *Body
func NewBody(name, desc string, lists []*List) *Body { // NewBody

	return &Body{
		Index:   0,
		Name:    name,
		Desc:    desc,
		AddTime: time.Now().Unix(),
		UpTime:  time.Now().Unix(),
		List:    lists,
	}
}

// NewList @Description
// @Author wangsen 2022-08-03 17:47:19
// @Param method 		方法类型
// @Param title 		api名称
// @Param path 			api 路由
// @Param reqBodyOther  返回结构体
// @Param resBody       请求结构体
// @Return *List
func NewList(method, title, path string, reqBodyOther, resBody interface{}) *List { // NewList
	req_body_other := ""
	res_body := ""

	if reqBodyOther != nil && reflect.TypeOf(reqBodyOther).Kind() == reflect.Ptr {
		req_body_other = service.CreateBoby(reqBodyOther)

	}
	if resBody != nil && reflect.TypeOf(resBody).Kind() == reflect.Ptr {
		res_body = service.CreateBoby(resBody)

	}

	if path == "" {
		path = title
	}
	params := make([]ReqParams, 0)
	headers := make([]ReqHeader, 0)
	queries := make([]ReqQuery, 0)
	froms := make([]ReqBodyFrom, 0)
	tag := make([]Tag, 0)
	return &List{
		QueryPath: struct {
			Path   string      `json:"path"`
			Params []ReqParams `json:"params"`
		}{
			Path:   path,
			Params: params,
		},
		EditUid:             0,
		Status:              "undone",
		Type:                "static",
		ReqBodyIsJsonSchema: true,
		ResBodyIsJsonSchema: true,
		ApiOpened:           false,
		Index:               0,
		Tag:                 tag,
		Id:                  snowflake.GenID(),
		Title:               title,
		Path:                path,
		Method:              method,
		ReqQuery:            queries,
		ReqBodyType:         "json",
		ResBodyType:         "json",
		ResBody:             res_body,
		ReqHeaders:          headers,
		ProjectId:           21,
		Catid:               snowflake.GenID(),
		ReqParams:           params,
		Uid:                 snowflake.GenID(),
		AddTime:             time.Now().Unix(),
		UpTime:              time.Now().Unix(),
		ReqBodyForm:         froms,
		V:                   0,
		Desc:                "",
		Markdown:            "",
		ReqBodyOther:        req_body_other,
	}
}
